package cn.petplatform.petback.service.sysmessage.impl;

import cn.petplatform.dto.ResponseDto;
import cn.petplatform.petback.exceptions.DataTransformException;
import cn.petplatform.petback.mapper.PetPublishMapper;
import cn.petplatform.petback.mapper.SysMessageMapper;
import cn.petplatform.petback.mapper.SysMsgWithUserAndPetMapper;
import cn.petplatform.petback.pojo.*;
import cn.petplatform.petback.pojo.vo.PublishPetDetailVo;
import cn.petplatform.petback.pojo.vo.SystemMessageVo;
import cn.petplatform.petback.service.sysmessage.SysMessageService;
import cn.petplatform.utils.JWTUtils;
import cn.petplatform.utils.UUIDUtils;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author LNL
 * @date 2021/5/14 22:43
 */
@Service("sysMessageService")
@Transactional
public class SysMessageServiceImpl implements SysMessageService {


    @Autowired
    private SysMsgWithUserAndPetMapper sysMsgWithUserAndPetMapper;

    @Autowired
    private SysMessageMapper sysMessageMapper;

    @Autowired
    private PetPublishMapper petPublishMapper;


//    获取系统消息的业务流程
    @Override
    public ResponseDto getAllSystemMessages() {
//        响应实体类
        ResponseDto responseDto = null;
        List<SystemMessageVo> systemMessageVos = sysMsgWithUserAndPetMapper.getAllSystemMessages();
        Optional<List<SystemMessageVo>> optionalList = Optional.ofNullable(systemMessageVos);
        if (optionalList.isPresent()) {
            responseDto = new ResponseDto("1000", "系统消息", systemMessageVos);
        } else {
            throw new DataTransformException("数据异常，请联系管理员");
        }
        return responseDto;
    }

    @Override
    public ResponseDto updateSystemMessage(SysMessage sysMessage, HttpServletRequest request) {
        ResponseDto responseDto = null;
        try {
            //获取token中的存储信息
            DecodedJWT dataUser = JWTUtils.getUserInfo(request.getHeader("token"));
            //系统消息获取之后，添加修改信息
            sysMessage.setEditTime(new Date());
            sysMessage.setEditUser(dataUser.getClaim("userId").asString());

            int isUpdate = sysMessageMapper.updateByPrimaryKeySelective(sysMessage);
            if (isUpdate > 0) {
                responseDto = new ResponseDto("1000", "修改成功");
            } else {
                responseDto = new ResponseDto("1001", "修改失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("修改失败,请重试");
        }
        return responseDto;
    }

    @Override
    public ResponseDto insertSystemMessage(SysMessage sysMessage, HttpServletRequest request) {
        ResponseDto responseDto = null;
        //当我们调用增加系统消息时，就有宠物发布
        DecodedJWT currentUserInfo = JWTUtils.getUserInfo(request.getHeader("token"));
        //如果不为空
        sysMessage.setId(UUIDUtils.getUUID());
        if (!"".equals(currentUserInfo.getClaim("userId").asString())) {
            sysMessage.setUserId(currentUserInfo.getClaim("userId").asString());
            sysMessage.setCreateUser(currentUserInfo.getClaim("userId").asString());
        }
        sysMessage.setCreateTime(new Date());
        sysMessage.setIsDeleted(1);
        //新增
        int isInsertMsg = sysMessageMapper.insertSelective(sysMessage);

        //同时进行审核，对tbl_publish表进行审核
        int isPass = petPublishMapper.updateIsPass(sysMessage.getPetId());

        if (isInsertMsg > 0 && isPass > 0) {
            responseDto = new ResponseDto("1000", "系统消息发布成功");
        } else {
            responseDto = new ResponseDto("1001", "系统消息发布失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto deleteSystemMessage(String id) {
        ResponseDto responseDto = null;
        SysMessage sysMessage = new SysMessage();
        sysMessage.setIsDeleted(0);
        sysMessage.setId(id);
        int isDelete = sysMessageMapper.updateByPrimaryKeySelective(sysMessage);
        if(isDelete > 0) {
            responseDto = new ResponseDto("1000","删除成功");
        }else {
            responseDto = new ResponseDto("1001","删除失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto getSystemMessagesById(String id) {
        ResponseDto responseDto=null;
        SysMessage sysMessage = sysMessageMapper.selectByPrimaryKey(id);
        responseDto=new ResponseDto("1000","数据获取成功",sysMessage);
        return responseDto;
    }

    @Override
    public ResponseDto getPublishIsPassAsZero() {
        ResponseDto responseDto = null;
        List<PublishPetDetailVo> publishes = petPublishMapper.selectIsPassAsZero();
        Optional<List<PublishPetDetailVo>> optionalList = Optional.ofNullable(publishes);
        if(optionalList.isPresent()) {
            responseDto = new ResponseDto("1000","获取数据成功",publishes);
        }else {
            responseDto = new ResponseDto("1001","获取数据失败");

        }
        return responseDto;
    }


}
